// TextProcess.cpp : implementation file
//

#include "stdafx.h"
#include "ImageLab.h"
#include "TextProcess.h"
#include "AnnotationTool.h"

// CTextProcess dialog

IMPLEMENT_DYNAMIC(CTextProcess, CDialog)

CTextProcess::CTextProcess(CWnd* pParent)
	: CDialog(CTextProcess::IDD, pParent)
{
	ASSERT(pParent!=NULL);
	parent = pParent;
	dialogid = CTextProcess::IDD;
	::AfxInitRichEdit2();
}

CTextProcess::~CTextProcess()
{
}

void CTextProcess::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CTextProcess::Create()
{
	return CDialog::Create(dialogid, parent);
}

//Set the free text
void CTextProcess::AssignStringFreeText(CString freetext)
{
	//Set the text to the window and assign to freetext
	CEdit* freetextedit = (CEdit*)CTextProcess::GetDlgItem(IDC_FREETEXT);
	CT2CA asciitext(freetext);
	mfreetext = string(asciitext);
	freetextedit->SetWindowTextW(freetext);

	return;
}

//Set the richedit box of keyterms
void CTextProcess::AssignStringKeyterms(CString keyterms)
{
	CEdit* keytermsedit = (CEdit*)CTextProcess::GetDlgItem(IDC_KEYTERMS);
	keytermsedit->SetWindowTextW(keyterms);
	return;
}

//Set the richedit box of keyterms for collocations
void CTextProcess::AssignStringKeytermsForCollocation(CString keyterms)
{
	CEdit* keytermsedit = (CEdit*)CTextProcess::GetDlgItem(IDC_KEYTERMSFORCOLLOCATION);
	keytermsedit->SetWindowTextW(keyterms);
	return;
}

//Get words for collocations from the collocation richedit box
vector<string> CTextProcess::GetWordsForCollocation(void)
{
	//Get the string
	CEdit* keytermsedit = (CEdit*)CTextProcess::GetDlgItem(IDC_KEYTERMSFORCOLLOCATION);
	CString multicharstring;
	keytermsedit->GetWindowTextW(multicharstring);
	bf.WriteToFile(CString("C:\\temp.txt"), multicharstring);

	//Return the keywords
	return bf.LoadWordsFromString(string("C:\\temp.txt"));
}

//Set the richedit box of collocation results
void CTextProcess::AssignStringCollocations(CString collocations)
{
	CEdit* collocationsedit = (CEdit*)CTextProcess::GetDlgItem(IDC_COLLOCATIONS);
	collocationsedit->SetWindowTextW(collocations);
	return;
}

void CTextProcess::OnOK()
{
	return;
}

afx_msg void CTextProcess::OnClose()
{
	CDialog::OnClose();
	if(parent)
	{
		CButton* button = (CButton*)(((CAnnotationTool*)parent)->GetDlgItem(IDC_BUTTON_TEXTPROCESS));
		button->SetWindowTextW(CString("Show Process"));
	}
	return;
}

void CTextProcess::PostNcDestroy()
{
	//Reset the pointer in the parent
	if(parent)
		((CAnnotationTool*)parent)->textprocess = NULL;
	//Destroy the window
	CTextProcess::DestroyWindow();
	return;
}

BEGIN_MESSAGE_MAP(CTextProcess, CDialog)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_DOWORDLIST, &CTextProcess::OnBnClickedDowordlist)
	ON_BN_CLICKED(IDC_DOCOLLOCATION, &CTextProcess::OnBnClickedDocollocation)
	ON_BN_CLICKED(IDC_GETALLTEXT, &CTextProcess::OnBnClickedGetalltext)
	ON_BN_CLICKED(IDC_TEXTPROCESSING_ANNOTATEIMAGE, &CTextProcess::OnBnClickedTextprocessingAnnotateimage)
	ON_EN_SETFOCUS(IDC_FREETEXT, &CTextProcess::OnEnSetfocusFreetext)
	ON_EN_SETFOCUS(IDC_KEYTERMS, &CTextProcess::OnEnSetfocusKeyterms)
	ON_EN_SETFOCUS(IDC_KEYTERMSFORCOLLOCATION, &CTextProcess::OnEnSetfocusKeytermsforcollocation)
	ON_EN_SETFOCUS(IDC_COLLOCATIONS, &CTextProcess::OnEnSetfocusCollocations)
END_MESSAGE_MAP()


//Do the word list
void CTextProcess::OnBnClickedDowordlist()
{
	CTextProcess::ResetAll();

	if(mfreetext.size()<=1)
		return;

	std::ostringstream systemstring1, systemstring2;

	//Get the key words and output to the window
	mkeywords = bf.LoadWordsFromString(mfreetext, mkeywordfrequency);
	for(unsigned int i=0; i<mkeywords.size(); i++)
		systemstring1 << mkeywords[i] << "\n";
	CTextProcess::AssignStringKeyterms(CString(systemstring1.str().c_str()));

	//Select the keywords and set them to the richedit box of keywordforcollocation
	mkeywordweirdness = bf.ComputerWeirdness(mkeywords, mkeywordfrequency);
	mkeywordsforcollocation = bf.SelectKeyterms(mkeywords, mkeywordfrequency, mkeywordweirdness, 0);
	for(unsigned int i=0; i<mkeywordsforcollocation.size(); i++)
		systemstring2 << mkeywordsforcollocation[i] << "\n";
	CTextProcess::AssignStringKeytermsForCollocation(CString(systemstring2.str().c_str()));

	return;
}

//Do the collocations
void CTextProcess::OnBnClickedDocollocation()
{
	//Reset the collocations
	CTextProcess::ResetCollocation();

	//Get the text from the collocation richedit box and do the collocations
	mkeywordsforcollocation = CTextProcess::GetWordsForCollocation();
	mcollocationlocation = bf.Collocations(mfreetext, mkeywordsforcollocation, mcollocations, mcollocationfrequency,
		mcollocationspread, mcollocationsum);

	//Output the results
	unsigned int wordcounter = 0;
	std::ostringstream systemstring;
	for(unsigned int i=0; i<mcollocations.size(); i++)
	{
		if(i==mcollocationlocation[wordcounter])
			wordcounter ++;
		systemstring << mkeywordsforcollocation[wordcounter] << ' ' << mcollocations[i] << "\n";
	}
	CTextProcess::AssignStringCollocations(CString(systemstring.str().c_str()));
	
	return;
}

//Reset all parameters
void CTextProcess::ResetAll()
{
	//Clear the dialog
	CTextProcess::AssignStringCollocations(CString(""));
	CTextProcess::AssignStringKeyterms(CString(""));
	CTextProcess::AssignStringKeytermsForCollocation(CString(""));

	//Clear the global parameters
	mkeywords.clear();
	mkeywordsforcollocation.clear();
	mkeywordfrequency.clear();
	mcollocationspread.clear();
	mcollocationfrequency.clear();
	mcollocations.clear();
	mkeywordweirdness.clear();
	mcollocationsum.clear();

	return;
}

//Reset collocations
void CTextProcess::ResetCollocation(void)
{
	//Clear the dialog
	CTextProcess::AssignStringCollocations(CString(""));

	//Clear the global parameters
	mcollocationspread.clear();
	mcollocationfrequency.clear();
	mcollocations.clear();
	mcollocationsum.clear();

	return;
}

void CTextProcess::OnBnClickedGetalltext()
{
	//Merge the text and send to freetext
	CString stringforfreetext;
	if(parent)
	{
		vector<string> filelist = ((CAnnotationTool*)parent)->GetFileList();
		for(unsigned int i=0; i<filelist.size(); i++)
		{
			filelist[i].resize(filelist[i].size()-4);
			string filename = filelist[i]+".txt";
			CFile cfile;
			CFileException e;
			if(!cfile.Open(CString(filename.c_str()), CFile::modeRead, &e))		//If failed to open the file, skip to the next one
				continue;
			char* tempchars = new char[(UINT)cfile.GetLength()];
			cfile.Read(tempchars, (UINT)cfile.GetLength());

			//Merge the text
			stringforfreetext += CString(tempchars, (UINT)cfile.GetLength());
			stringforfreetext += CString("\r\n");
			cfile.Close();
		}
	}
	
	CTextProcess::AssignStringFreeText(stringforfreetext);
	return;
}


void CTextProcess::OnBnClickedTextprocessingAnnotateimage()
{
	//Get the editbox that get the last focus, and get the selected text
	CEdit* edit= (CEdit*)CTextProcess::GetDlgItem(dialogselected);
	int selectstart, selectend;
	edit->GetSel(selectstart, selectend);
	string textselected;

	if(selectend-selectstart>2)		//If text is selected, get the text
	{
		CString multicharstring;
		edit->GetWindowTextW(multicharstring);
		CT2CA ascii(multicharstring);
		string asciistring(ascii);
		CTextProcess::RemoveEnter(asciistring);
		textselected = string(&(asciistring.c_str()[selectstart]), selectend-selectstart);
	}
	else
	{
		::AfxMessageBox(CString("Selected text to small to be key words"), MB_OK, 0);
		return;
	}

	//Remove the last char if it is a \n
	string::iterator stringiterator = --textselected.end();
	if(*stringiterator=='\n'||*stringiterator==' '||*stringiterator=='\t')
		textselected.erase(stringiterator);

	((CAnnotationTool*)parent)->AppendKeyword(textselected);
	edit->SetFocus();

	return;
}

//Remove the enter char from a CString
void CTextProcess::RemoveEnter(string &enterstring)
{
	string::iterator stringiterator;
	for(stringiterator = enterstring.begin(); stringiterator!=enterstring.end(); stringiterator ++)
	{
		if(*stringiterator=='\r')		//If a enter char found, remove it
			enterstring.erase(stringiterator);
	}
	return;
}

//If freetext editbox get focus
void CTextProcess::OnEnSetfocusFreetext()
{
	dialogselected = IDC_FREETEXT;
}

//If keyterm editbox get focus
void CTextProcess::OnEnSetfocusKeyterms()
{
	dialogselected = IDC_KEYTERMS;
}

//If the keyterm for collocation edit box get focus
void CTextProcess::OnEnSetfocusKeytermsforcollocation()
{
	dialogselected = IDC_KEYTERMSFORCOLLOCATION;
}

//If the collocation edit box get focus
void CTextProcess::OnEnSetfocusCollocations()
{
	dialogselected = IDC_COLLOCATIONS;
}

//Close the dialog
void CTextProcess::CloseDialog()
{
	CTextProcess::OnClose();
}